home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / k_r.arc / K&R_CH7.C < prev    next >
Text File  |  1985-11-05  |  8KB  |  663 lines

  1. pr 7.*
  2.  
  3.  
  4. Jul 27 17:12 1984  7.14getdbl_n.c Page 1
  5.  
  6.  
  7. #include <stdio.h>
  8. main()                  /* test getdbl_n() */
  9. {       double getdbl_n();
  10.         printf("number is %f\n", getdbl_n());
  11. }
  12. double  getdbl_n()      /* convert ASCII to dbl - 2nd version */
  13. {       int     c, sign = 1;    /* c = char input, sign = + */
  14.         float   d = 1.0;        /* d = decimal place */
  15.         double  num = 0.0;      /* num = converted number */
  16.         printf("input number: ");
  17.         if      ((c = getc(stdin)) == '-')
  18.                 sign = -1;              /* sign = - */
  19.         else if (c == '\n' || c == EOF)
  20.                 return (0);
  21.         else if (c == '.' || (c >= '0' && c <= '9'))
  22.                 ungetc(c, stdin);       /* return c to buffer */
  23.         while ((c = getc(stdin)) != '\n' && c != '.' && c != EOF)
  24.                 if (c >= '0' && c <= '9')
  25.                         num = num * 10 + (c - '0');
  26.         if (c == '.')
  27.                 while ((c = getc(stdin)) != EOF && c != '\n')
  28.                         if (c >= '0' && c <= '9')
  29.                         { d /= 10.0; num += d * (c - '0');
  30.                         }
  31.         return (num * sign);
  32. }
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70. Jul 27 17:12 1984  7.20cp.c Page 1
  71.  
  72.  
  73. #include <stdio.h>
  74. main(argc, argv)        /* modified cp(1) command */
  75. int argc;
  76. char *argv[];
  77. { FILE *fpr, *fpw;      /* pointers to type FILE */
  78.   if (argc != 3)
  79.   { printf("usage: %s src_file dest_file\n", argv[0]);
  80.     exit(1);
  81.   }
  82.   if ((fpr = fopen(argv[1], "r")) == NULL)
  83.   { printf("%s: cannot access %s\n", argv[0], argv[1]);
  84.     exit(2);
  85.   }
  86.   if ((fpw = fopen(argv[2], "w")) == NULL)
  87.   { printf("%s: cannot create %s\n", argv[0], argv[2]);
  88.     exit(2);
  89.   }
  90.   if (copy(fpr, fpw) == EOF)
  91.         exit(0);        /* success */
  92.   exit(3);              /* failure */
  93. }
  94. copy(fp1, fp2)          /* copy fp1 to fp2 */
  95. FILE *fp1, *fp2;
  96. {
  97.         int c;
  98.  
  99.         while ((c = getc(fp1)) != EOF)
  100.                 putc(c, fp2);
  101.         return (c);
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. Jul 27 17:12 1984  7.27cat.c Page 1
  137.  
  138.  
  139. #include <stdio.h>
  140. main(argc, argv)        /* modified cat(1) command */
  141. int argc;
  142. char *argv[];
  143. {
  144.   register i, c;
  145.   FILE *fp;             /* fp is pointer to type FILE */
  146.  
  147.   for (i = 1; i < argc; ++i)
  148.   { if ((fp = fopen(argv[i], "r")) == NULL)
  149.     {   fprintf(stderr, "%s: cannot open %s\n",
  150.                 argv[0], argv[i]);
  151.         continue;
  152.     }
  153.     while ((c = getc(fp)) != EOF)
  154.         putc(c, stdout);
  155.     fclose(fp);
  156.   }
  157. }
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. Jul 27 17:12 1984  7.34sscanf.c Page 1
  203.  
  204.  
  205. #include <stdio.h>
  206. char tst[] = "123456.78 592 89000 now is the time";
  207. main()  /* demonstration of sscanf() */
  208. {
  209.   int   dec;
  210.   float f;
  211.   long  lg;
  212.   char  s1[50], s2[50];
  213.   int ret;
  214.  
  215.   ret = sscanf(tst, "%3d %f %*d %ld %s %[a-z ]",
  216.                 &dec, &f, &lg, s1, s2);
  217.   printf("return from sscanf() was %d\n", ret);
  218.   printf("int dec is %d\n", dec);
  219.   printf("float f is %.2f\n", f);
  220.   printf("long lg is %ld\n", lg);
  221.   printf("array s1 is \"%s\"\n", s1);
  222.   printf("array s2 is \"%s\"\n", s2);
  223. }
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. Jul 27 17:12 1984  7.46atof.c Page 1
  269.  
  270.  
  271. /* another version of getdbl() */
  272.  
  273. #include <stdio.h>
  274. #define MAX 256
  275. main()  /* test getdbl_a() */
  276. {       double getdbl_a();
  277.  
  278.         printf("num is %f\n", getdbl_a());
  279. }
  280. double getdbl_a()       /* use (3S) & (3C) functions */
  281. {       char stor[MAX];
  282.         double atof();
  283.  
  284.         printf("input number: ");
  285.         return (atof(fgets(stor, MAX, stdin)));
  286. }
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334. Jul 27 17:12 1984  7.50create.c Page 1
  335.  
  336.  
  337. #include <stdio.h>
  338. #define SCORE 8
  339. struct record
  340. {       char name[48];
  341.         float score[SCORE];
  342. };
  343. main()  /* create DATAFILE with student names and scores */
  344. {       struct record data;
  345.         FILE *fp;
  346.         if ((fp = fopen("DATAFILE", "w")) == NULL)
  347.         {       fprintf(stderr, "cannot create DATAFILE\n");
  348.                 exit(2);
  349.         }
  350.         while (load(&data))
  351.                 fwrite(&data, sizeof (data), 1, fp);
  352.         exit(0);
  353. }
  354. load(pstr)      /* get data from terminal; load structure */
  355. struct record *pstr;
  356. {
  357.         register i;
  358.         printf("\ninput name: ");
  359.         gets(pstr->name);
  360.         if (pstr->name[0] == '\0')
  361.                 return (0);
  362.         printf("input up to %d scores ('q' to quit): ", SCORE);
  363.         for (i = 0; i < SCORE ; ++i)
  364.                 if (scanf("%f", &pstr->score[i]) != 1)
  365.                 {       pstr->score[i] = EOF;
  366.                         break;
  367.                 }
  368.         while (getchar() != '\n')       /* buffer drain */
  369.                 ;
  370.         return (1);
  371. }
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400. Jul 27 17:12 1984  7.52getinfo.c Page 1
  401.  
  402.  
  403. #include <stdio.h>
  404. #define SCORE 8
  405. struct record
  406. {       char name[48];
  407.         float score[SCORE];
  408. };
  409. main()  /* get information from DATAFILE */
  410. {       struct record data;
  411.         FILE *fp;
  412.         if ((fp = fopen("DATAFILE", "r")) == NULL)
  413.         {       fprintf(stderr, "cannot open DATAFILE\n");
  414.                 exit(2);
  415.         }
  416.         while (fread(&data, sizeof (data), 1, fp))
  417.                 print(&data);
  418.         exit(0);
  419. }
  420. print(pstr)     /* compute student ave; provide printout */
  421. struct record *pstr;
  422. {
  423.         register i, cnt = 0;
  424.         double num = 0.0;
  425.         for (i = 0; i < SCORE ; ++i)
  426.                 if      (pstr->score[i] != EOF)
  427.                 {       printf("%7.2f", pstr->score[i]);
  428.                         num += pstr->score[i];
  429.                         ++cnt;
  430.                 }
  431.                 else    for (   ; i < SCORE; ++i)
  432.                                 printf("   --- ");
  433.         if      (cnt > 0)
  434.                 printf(" (%6.2f)", num / (double) cnt);
  435.         else    printf(" (  --- )");
  436.         printf(" %s\n", pstr->name);
  437. }
  438.  
  439.  
  440.  
  441.  
  442.  
  443.  
  444.  
  445.  
  446.  
  447.  
  448.  
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.  
  456.  
  457.  
  458.  
  459.  
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466. Jul 27 17:12 1984  7.56checknum.c Page 1
  467.  
  468.  
  469. #include <stdio.h>
  470. #define SCORE 8
  471. struct record
  472. {       char name[48];
  473.         float score[SCORE];
  474. };
  475. main()  /* check number of entries in DATAFILE */
  476. {       struct record data;
  477.         FILE *fp;
  478.         if ((fp = fopen("DATAFILE", "r")) == NULL)
  479.         {       fprintf(stderr, "cannot open DATAFILE\n");
  480.                 exit(2);
  481.         }
  482.         fseek(fp, 0L, 2);       /* move pointer to end-of-file */
  483.         printf("%5ld entries in DATAFILE\n", ftell(fp) / (long) sizeof (data));
  484.         exit(0);
  485. }
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532. Jul 27 17:12 1984  7.59system.c Page 1
  533.  
  534.  
  535. main()  /* use sh(1) to get
  536.         /* long listing of DATAFILE
  537.         /* and use same exit status */
  538. {
  539.         unsigned ret;   /* machine independent */
  540.  
  541.         ret = system("ls -l DATAFILE");
  542.         exit(ret >> 8); /* shift right 8 places
  543.                         /* to get sh(1) exit status */
  544. }
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.  
  560.  
  561.  
  562.  
  563.  
  564.  
  565.  
  566.  
  567.  
  568.  
  569.  
  570.  
  571.  
  572.  
  573.  
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.  
  583.  
  584.  
  585.  
  586.  
  587.  
  588.  
  589.  
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598. Jul 27 17:12 1984  7.70cp2.c Page 1
  599.  
  600.  
  601. #include <fcntl.h>      /* included for open(2) */
  602. main(argc, argv)        /* modified cp(1) command */
  603. int argc;               /* 2nd version */
  604. char *argv[];           /* using system calls */
  605. { int fdr, fdw;         /* file descriptors */
  606.   if (argc != 3)
  607.   { printf("usage: %s src_file dest_file\n", argv[0]);
  608.     exit(1);
  609.   }
  610.   if ((fdr = open(argv[1], 0)) == -1)
  611.   { printf("%s: cannot access %s\n", argv[0], argv[1]);
  612.     exit(2);
  613.   }
  614.   if ((fdw = creat(argv[2], 0644)) == -1)
  615.   { printf("%s: cannot create %s\n", argv[0], argv[2]);
  616.     exit(2);
  617.   }
  618.   if (copy(fdr, fdw) == 0)
  619.         exit(0);        /* success */
  620.   exit(3);              /* failure */
  621. }
  622. copy(fd1, fd2)          /* copy fd1 to fd2 */
  623. int fd1, fd2;
  624. { register n;
  625.   char buf[512];        /* temp character storage */
  626.  
  627.   while ((n = read(fd1, buf, 512)) > 0)
  628.     write(fd2, buf, n);
  629.   return (n);
  630. }
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643.  
  644.  
  645.  
  646.  
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658.  
  659.  
  660.  
  661.  
  662.